package com.jhgsys.internal.incentive.service.impl;

import com.jhgsys.internal.incentive.entity.IncentiveQuotaGoods;
import com.jhgsys.internal.incentive.mapper.IncentiveQuotaGoodsMapper;
import com.jhgsys.internal.incentive.service.IIncentiveQuotaGoodsService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-09-02 10:39:13
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveQuotaGoodsServiceImpl extends ServiceImpl<IncentiveQuotaGoodsMapper, IncentiveQuotaGoods> implements IIncentiveQuotaGoodsService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     * @param request QueryRequest
     * @param incentiveQuotaGoods incentiveQuotaGoods
     * @return IPage<IncentiveQuotaGoods>
    */
    @Override
    public IPage<IncentiveQuotaGoods> findIncentiveQuotaGoodsPage(QueryRequest request, IncentiveQuotaGoods incentiveQuotaGoods) {
        LambdaQueryWrapper<IncentiveQuotaGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveQuotaGoods::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<IncentiveQuotaGoods> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     * @param incentiveQuotaGoods incentiveQuotaGoods
     * @return List<IncentiveQuotaGoods>
     */
    @Override
    public List<IncentiveQuotaGoods> findIncentiveQuotaGoodsList(IncentiveQuotaGoods incentiveQuotaGoods) {
	    LambdaQueryWrapper<IncentiveQuotaGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveQuotaGoods::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(incentiveQuotaGoods.getQuotaConfigId())){
            queryWrapper.eq(IncentiveQuotaGoods::getQuotaConfigId,incentiveQuotaGoods.getQuotaConfigId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-09-02 10:39:13
    * @param id
    * @return IncentiveQuotaGoods
    */
    @Override
    public IncentiveQuotaGoods findIncentiveQuotaGoodsById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     * @param incentiveQuotaGoods incentiveQuotaGoods
     */
    @Override
    @Transactional
    public void createIncentiveQuotaGoods(IncentiveQuotaGoods incentiveQuotaGoods) {
        incentiveQuotaGoods.setCreatedTime(new Date());
        this.save(incentiveQuotaGoods);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     * @param incentiveQuotaGoods incentiveQuotaGoods
     */
    @Override
    @Transactional
    public void updateIncentiveQuotaGoods(IncentiveQuotaGoods incentiveQuotaGoods) {
        incentiveQuotaGoods.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveQuotaGoods);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-09-02 10:39:13
     * @param incentiveQuotaGoods incentiveQuotaGoods
     * @param incentiveQuotaGoodsIds incentiveQuotaGoodsIds
     */
    @Override
    @Transactional
    public void deleteIncentiveQuotaGoods(IncentiveQuotaGoods incentiveQuotaGoods,String incentiveQuotaGoodsIds) {
        List<String> list = Arrays.asList(incentiveQuotaGoodsIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveQuotaGoods,new LambdaQueryWrapper<IncentiveQuotaGoods>().in(IncentiveQuotaGoods::getQuotaGoodsId,list));
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-09-02 10:39:13
    * @param incentiveQuotaGoodsIds incentiveQuotaGoodsIds
    */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveQuotaGoods(String incentiveQuotaGoodsIds) {
        List<String> list = Arrays.asList(incentiveQuotaGoodsIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveQuotaGoods> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveQuotaGoods::getQuotaGoodsId,list);
        this.remove(wapper);
    }

    @Override
    public void deleteInceniveQUatGoodsById(String configId,String currentUser) {
        List<String> list = Arrays.asList(configId.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveQuotaGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(IncentiveQuotaGoods::getQuotaConfigId,list);
        this.remove(wrapper);
    }
}
